<!--
 * @Author: Rain
 * @Date: 2021-11-25 22:38:56
 * @LastEditTime: 2024/01/15 16:30:36
 * @Description: Do Something
-->
<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div class="flex items-center flex-end">
        <!-- 搜索 -->
        <el-input
          v-model="query.orderNo"
          clearable
          placeholder="订单编号"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select
          v-model="query.payStatus"
          placeholder="支付状态"
          style="margin: 0 5px"
          clearable
          @change="crud.toQuery"
        >
          <el-option label="未支付" :value="0" />
          <el-option label="已支付" :value="1" />
        </el-select>
        <el-select
          v-model="query.atmrId"
          placeholder="测试单元"
          style="margin: 0 5px"
          clearable
          @change="crud.toQuery"
        >
          <el-option
            v-for="(item, index) in atmrList"
            :key="index"
            :label="item.atmrName"
            :value="item.id"
          />
        </el-select>
        <el-date-picker
          v-model="query.createTime"
          type="daterange"
          range-separator="至"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="crud.toQuery"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="80px"
        />
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
          >确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%"
        align="center"
        show-summary
        :summary-method="getSummaries"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column prop="openId" label="微信用户" align="center" />
        <el-table-column prop="wxUser.mobile" label="手机号" align="center" />
        <el-table-column prop="orderNo" label="订单编号" align="center" />
        <el-table-column prop="createTime" label="订单时间" align="center" />
        <el-table-column prop="orderAmount" label="订单金额" align="center" />
        <el-table-column prop="payAmount" label="支付金额" align="center" />
        <el-table-column prop="discountAmount" label="优惠金额" align="center" />
        <el-table-column prop="prePayid" label="支付流水号" align="center" />
        <el-table-column prop="payStatus" label="支付状态" align="center">
          <template slot-scope="{ row }">
            {{ row.payStatus * 1 === 0 ? "未支付" : "已支付" }}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="orderStatus" label="订单状态" align="center" /> -->
        <el-table-column prop="teamTestid" label="团体免费测评" align="center">
          <template slot-scope="{ row }">
            {{ row.teamTestid * 1 === 0 ? "否" : "是" }}
          </template>
        </el-table-column>
        <!-- <el-table-column
          v-if="checkPer(['admin', 'examOrder:edit', 'examOrder:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column> -->
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudExamOrder from '@/api/examOrder'
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import pagination from '@crud/Pagination'
import rrOperation from '@crud/RR.operation'

const defaultForm = {
  orderId: null,
  orderno: null,
  createtime: null,
  orderamount: null,
  payamount: null,
  discountamount: null,
  openid: null,
  atmrid: null,
  prepayid: null,
  paystatus: null,
  orderstatus: null,
  teamtest: null,
  updatetime: null,
  payserialid: null
}
export default {
  name: 'ExamOrder',
  components: { pagination, rrOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '订单',
      url: 'api/examOrder',
      idField: 'orderId',
      sort: 'orderId,desc',
      crudMethod: { ...crudExamOrder }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'examOrder:add'],
        edit: ['admin', 'examOrder:edit'],
        del: ['admin', 'examOrder:del']
      },
      rules: {
        orderno: [{ required: true, message: '订单编号不能为空', trigger: 'blur' }],
        createtime: [{ required: true, message: '订单时间不能为空', trigger: 'blur' }],
        orderamount: [{ required: true, message: '订单金额不能为空', trigger: 'blur' }],
        payamount: [{ required: true, message: '支付金额不能为空', trigger: 'blur' }],
        discountamount: [
          { required: true, message: '优惠金额不能为空', trigger: 'blur' }
        ],
        atmrid: [{ required: true, message: '测试id不能为空', trigger: 'blur' }],
        prepayid: [{ required: true, message: '支付流水号不能为空', trigger: 'blur' }],
        paystatus: [
          {
            required: true,
            message: '支付状态，0：未支付，1：已支付不能为空',
            trigger: 'blur'
          }
        ],
        orderstatus: [{ required: true, message: '订单状态不能为空', trigger: 'blur' }],
        teamtest: [
          {
            required: true,
            message: '是否团体免费测评0：不是，1L是不能为空',
            trigger: 'blur'
          }
        ],
        payserialid: [{ required: true, message: '支付流水号不能为空', trigger: 'blur' }]
      },
      queryTypeOptions: [{ key: 'orderno', display_name: '订单编号' }],
      atmrList: []
    }
  },
  mounted() {
    this.$http.get('/api/atmr?page=0&size=100000&sort=id').then((_) => {
      const { content } = _
      this.atmrList = content
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总价'
          return
        } else if (index === 3 || index === 4) {
          const values = data.map((item) => Number(item[column.property]))
          if (!values.every((value) => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            sums[index] = Number(sums[index]).toFixed(2)
            sums[index] += ' 元'
          } else {
            sums[index] = ''
          }
        }
      })

      return sums
    }
  }
}
</script>

<style scoped></style>
